//
//  IGAddToDrawVC.m
//  jingdongfang
//
//  Created by BOE on 2018/5/22.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "IGAddToDrawVC.h"
#import "IGAddToDrawApi.h"
#import "IGMyDrawingListApi.h"
#import "IGCreatPictureListVC.h"
#import "IGDrawListCell.h"

@interface IGAddToDrawVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIView *addDrawView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *drawList;

@end

@implementation IGAddToDrawVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的画单";
    self.drawList = [NSMutableArray array];
    
    [self createUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self dataRequest];
}

- (void)createUI {
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        if ([[[UIDevice currentDevice] systemVersion] floatValue]>= 11.0)
        {
            _tableView.estimatedRowHeight = 0;
            _tableView.estimatedSectionHeaderHeight = 0;
            _tableView.estimatedSectionFooterHeight = 0;
        }
    }
    return _tableView;
}

- (UIView *)addDrawView {
    if (!_addDrawView) {
        _addDrawView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 75)];
        
        UIImageView *addImageView = [UIImageView new];
        addImageView.backgroundColor = [UIColor appColor];
        [_addDrawView addSubview:addImageView];
        
        UILabel *addLabel = [UILabel new];
        addLabel.font = [UIFont font18];
        addLabel.textColor = [UIColor appColor];
        addLabel.text = @"新建画单";
        [_addDrawView addSubview:addLabel];
        
        [addImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.mas_equalTo(0);
            make.width.mas_equalTo(75);
        }];
        
        [addLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(addImageView.mas_right).mas_offset(24);
            make.right.mas_equalTo(20);
            make.top.bottom.mas_equalTo(0);
        }];
        
    }
    return _addDrawView;
}

- (void)dataRequest {
    __weak typeof(self) weakSelf = self;
    IGMyDrawingListApi *drawListApi = [[IGMyDrawingListApi alloc] initWithType:DRAWINGLIST_MYCREATE_TYPE];
    drawListApi.pageNum = @"1";
    drawListApi.pageSize = @"50";
    [drawListApi startWithDecodeClass:[IGMyDrawingListResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGMyDrawingListResp *resp = responseObject;
            IGMyDrawingModel *model = resp.data;
            weakSelf.drawList = [NSMutableArray arrayWithArray:model.drawlist];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableView reloadData];
            });
        }
    }];
}

- (void)addtoDraw:(NSString *)drawId {
    IGAddToDrawApi *addToDrawApi = [IGAddToDrawApi new];
    addToDrawApi.drawId = drawId;
    addToDrawApi.workIds = self.workIds;
    
    __weak typeof(self) weakSelf = self;
    [addToDrawApi startWithDecodeClass:[IGAddToDrawResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            [weakSelf presentMessageTips:@"添加成功"];
        }
    }];
}

#pragma mark --<UITableViewDelegate, UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return self.drawList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"kUITableViewCell"];
        
        [cell addSubview:self.addDrawView];
        return cell;
    }
    
    IGDrawListCell *cell = [IGDrawListCell cellWithTable:tableView];
    IGDrawModel *drawModel = self.drawList[indexPath.row];
    cell.drawModel = drawModel;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 75.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    //新建画单
    if (indexPath.section == 0) {
        [UIViewController ig_pushViewControllerClass:[IGCreatPictureListVC class] animated:YES];
        return;
    }
    
    //添加到画单
    IGDrawModel *drawModel = self.drawList[indexPath.row];
    [self addtoDraw:drawModel.drawId];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
